﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YTCMS.Core
{
    public static class YTConvert
    {


        public static string AdminStateExtend(this string str)
        {
            if (str.Equals("正常"))
            {
                return "<span class='label label-success'>正常</span>";
            }
            else if (str.Equals("禁止"))
            {
                return "<span class='label label-default'>禁止</span>";
            }
            else if (str.Equals("删除"))
            {
                return "<span class='label label-danger'>删除</span>";
            }
            else
            {
                return "<span class='label label-warning'>默认</span>";
            }
        }
        public static string ExtendFile<T>(this T t, string fileName)
        {
            if (t == null)
                return "";
            else
                return typeof(T).GetProperty(fileName).GetValue(t).ToString();
        }


        public static DateTime? StrConvertDate(this string str,DateTime? defaulttime = null)
        {
            if (string.IsNullOrEmpty(str))
                return defaulttime;
            else
            {
                DateTime d;
                DateTime.TryParse(str, out d);
                return d;
            }
        }
        public static int? StrConvertInt(this string str,int? def=null)
        {
            if (string.IsNullOrEmpty(str))
                return def;
            else
            {
                int d;
                int.TryParse(str, out d);
                return d;
            }
        }
        public static int StrConvertIntDefault(this string str, int def = 0)
        {
            if (string.IsNullOrEmpty(str))
                return def;
            else
            {
                int d;
                int.TryParse(str, out d);
                return d;
            }
        }
        public static bool? StrConvertBool(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            else
            {
                bool d;
                Boolean.TryParse(str, out d);
                return d;
            }
        }
        public static decimal? StrConvertDecimal(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            else
            {
                decimal d;
                decimal.TryParse(str, out d);
                return d;
            }
        }
        public static string DateConvertStr(this DateTime? dt, string format)
        {
            if (dt == null)
                return null;
            else
                return dt.Value.ToString(format);
        }


        public static T WZTFirstOrDefault<T>(this IList<T> list, Func<T, bool> func)
        {
            if (list == null)
                return (T)Activator.CreateInstance(typeof(T));
            else
            {
                var first = list.FirstOrDefault(func);
                if (first == null)
                {
                    return (T)Activator.CreateInstance(typeof(T));
                }
                return first;
            }
        }

        public static IEnumerable<TSource> YTTake<TSource>(this IEnumerable<TSource> source, int count)
        {
            if (source == null)
                return null;
            else
            {
                var max = source.Count();
                if (max < count)
                {
                    return source.Take(max);
                }
                return source.Take(count);
            }
        }
        public static TSource YTFirst<TSource>(this IEnumerable<TSource> source)
        {
            if (source == null || source.Count() == 0)
                return default(TSource);
            else
            {
                return source.First();
            }
        }
        public static TSource YTCurrent<TSource>(this IEnumerable<TSource> source,int skip)
        {
            if (source == null)
                return default(TSource);
            else
            {
                return source.Skip(skip).YTFirst();
            }
        }
        public static IEnumerable<TSource> YTSkip<TSource>(this IEnumerable<TSource> source, int skip, int take)
        {
            if (source == null)
                return null;
            else
            {
                return source.Skip(skip).Take(take);
            }
        }

        public static string YTGetTags(this string source,string classtag)
        {
            if (string.IsNullOrEmpty(source))
                return "";
            else
            {
                return "<img src='" + source + "' class='" + classtag + "' />";
            }
            
        }
        /// <summary>
        /// 是和否的转换
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string ConvertBool(this sbyte? i)
        {
            if (i == null)
                return "否";
            else {
                if (i == 1)
                {
                    return "是";
                }
                else{
                    return "否";
                }
            }
        }

    }
}
